package com.suyafei.es.service.impl;

import com.suyafei.es.service.PositionService;
import com.suyafei.es.util.DBHelper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.bulk.BackoffPolicy;
import org.elasticsearch.action.bulk.BulkProcessor;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.ByteSizeUnit;
import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;

/**
 * @program: es-homework
 * @description
 * @author: luffy
 * @create: 2020-12-10 21:21
 */
@Service
public class PositionServiceImpl implements PositionService {

    private static final Logger logger = LogManager.getLogger(PositionServiceImpl.class);

    @Autowired
    private RestHighLevelClient client;

    private static final String POSITION_INDEX ="position";


    @Override
    public void importAll() throws IOException {
        writeMySqlDataToES(POSITION_INDEX);
    }

    @Override
    public List<Map<String, Object>> searchPos(String keyword, int pageNo, int pageSize) throws IOException {
        if (pageNo <= 1) {
            pageNo = 1;
        }
        //进行搜索

        SearchRequest searchRequest = new SearchRequest(POSITION_INDEX);
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

        //分页设置
        searchSourceBuilder.from((pageNo - 1 ) * pageSize);
        searchSourceBuilder.size(pageSize);
        MatchQueryBuilder positionName = QueryBuilders.matchQuery("positionName", keyword);
        searchSourceBuilder.query(positionName);
        searchSourceBuilder.timeout(new TimeValue(60L,TimeUnit.SECONDS));
        //执行搜索
        searchRequest.source(searchSourceBuilder);
        SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
        ArrayList<Map<String, Object>> list = new ArrayList<>();
        SearchHit[] hits = search.getHits().getHits();
        if (hits.length < 5) {
            for (SearchHit hit : hits) {
                list.add(hit.getSourceAsMap());
            }
            BoolQueryBuilder should = QueryBuilders.boolQuery()
                    .should(QueryBuilders.matchQuery("positionAdvantage", "美女多"))
                    .should(QueryBuilders.matchQuery("positionAdvantage", "员工福利好"));
            searchSourceBuilder.query(should);
            searchSourceBuilder.timeout(new TimeValue(60L,TimeUnit.SECONDS));
            searchRequest.source(searchSourceBuilder);
            SearchResponse search2 = client.search(searchRequest, RequestOptions.DEFAULT);
            SearchHit[] hits1 = search2.getHits().getHits();
            for (int i = 0; i <= 5 - hits.length; i++) {
                list.add(hits1[i].getSourceAsMap());
            }
        }else {
            for (SearchHit hit : hits) {
                list.add(hit.getSourceAsMap());
            }
        }

        return list;
    }

    private void writeMySqlDataToES(String tableName){
        BulkProcessor bulkProcessor = getBulkProcessor(client);

        Connection connection = null;

        PreparedStatement ps = null;

        ResultSet rs = null;

        try {
            connection = DBHelper.getConn();

            logger.info("开始处理数据" + tableName);

            String sql = "select * from " + tableName;

            ps = connection.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
            //设置fetchSize
            ps.setFetchSize(20);
            rs = ps.executeQuery();

            ResultSetMetaData colData = rs.getMetaData();
            ArrayList<HashMap<String, String>> dataList = new ArrayList<>();

            HashMap<String, String> map = null;
            int count = 0;
            //c列 v值
            String c = null;
            String v = null;

            while (rs.next()) {
                count++;
                map = new HashMap<String, String>(128);
                for (int i = 1; i < colData.getColumnCount(); i++) {
                    c = colData.getColumnName(i);
                    v = rs.getString(c);
                    map.put(c, v);
                }
                dataList.add(map);
                //每1w条写一次，不足一万最后一次提交
                if (count % 1000 == 0) {
                    logger.info("数据库处理数量" + count);
                    for (HashMap<String, String> hashMap2 : dataList) {
                        bulkProcessor.add(new IndexRequest(POSITION_INDEX).source(hashMap2));
                    }
                    //清理
                    map.clear();
                    dataList.clear();
                }
            }
            //处理未提交的 数据
            for (HashMap<String, String> hashMap2 : dataList) {
                bulkProcessor.add(new IndexRequest(POSITION_INDEX).source(hashMap2));
            }
            bulkProcessor.flush();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                rs.close();
                ps.close();
                connection.close();
                boolean b = bulkProcessor.awaitClose(150L, TimeUnit.SECONDS);
                logger.info(b);
            } catch (SQLException | InterruptedException throwables) {
                throwables.printStackTrace();
            }
        }

    }

    private BulkProcessor getBulkProcessor(RestHighLevelClient client) {
        BulkProcessor bulkProcessor = null;
        try {
            BulkProcessor.Listener listener = new BulkProcessor.Listener() {
                @Override
                public void beforeBulk(long l, BulkRequest bulkRequest) {
                    logger.info("试着插入的数量" + bulkRequest.numberOfActions());
                }

                @Override
                public void afterBulk(long l, BulkRequest bulkRequest, BulkResponse bulkResponse) {
                    logger.info("成功插入的数量" + bulkRequest.numberOfActions());
                }

                @Override
                public void afterBulk(long l, BulkRequest bulkRequest, Throwable throwable) {
                    logger.info("bulk 失败：" + throwable + "executionId" + l);
                }
            };
            BiConsumer<BulkRequest, ActionListener<BulkResponse>> bulkConsumer = (request, bulkListener) -> {
                client.bulkAsync(request, RequestOptions.DEFAULT, bulkListener);
            };
            BulkProcessor.Builder builder = BulkProcessor.builder(bulkConsumer, listener);

            builder.setBulkActions(5000);
            builder.setBulkSize(new ByteSizeValue(100L, ByteSizeUnit.MB));
            builder.setConcurrentRequests(10);
            builder.setFlushInterval(TimeValue.timeValueSeconds(100L));

            builder.setBackoffPolicy(BackoffPolicy.constantBackoff(TimeValue.timeValueSeconds(1L), 3));

            bulkProcessor = builder.build();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                bulkProcessor.awaitClose(100L, TimeUnit.SECONDS);
            } catch (Exception e1) {
                logger.error(e1.getMessage());
            }
        }
        return bulkProcessor;
    }
}
